#pragma once
#include <osgGA/GUIEventHandler>
#include <functional>

#define KEY(name) osgGA::GUIEventAdapter::KeySymbol::KEY_##name

class OGUIEventHandler : public osgGA::GUIEventHandler {
public:
	OGUIEventHandler(const std::function<void(osgGA::GUIEventAdapter::KeySymbol)>& callback): m_keyCallback(callback) {};
	OGUIEventHandler(const std::function<void()>& callback): m_eventCallback(callback) {};

	~OGUIEventHandler() { std::cout << ">>> ~OGUIEventHandler" << std::endl; }

	/**@return `true`: pass event on; `false`: event consumed here*/
	bool handle(const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& aa) override {
		auto eventType = ea.getEventType();

		if (eventType == osgGA::GUIEventAdapter::KEYDOWN)
			m_keyCallback(static_cast<osgGA::GUIEventAdapter::KeySymbol>(ea.getKey()));
		else if (eventType == osgGA::GUIEventAdapter::DRAG || eventType == osgGA::GUIEventAdapter::SCROLL)
			m_eventCallback();
		return false;
	}

private:
	std::function<void(osgGA::GUIEventAdapter::KeySymbol)> m_keyCallback;
	std::function<void()> m_eventCallback = []{};
};